/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.pingan.mobilegw.core.service.util;

import com.alibaba.common.lang.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * did工具类
 * 
 * @author jiadao
 * @version $Id: DidUtil.java, v 0.1 2013-5-9 下午12:42:28 jiadao Exp $
 */
public class DidUtil {

    /** logger */
    private static final Log logger = LogFactory.getLog(DidUtil.class);

    /**
     * 获取did去除IMSI（IMEI|ClientKey）
     * 
     * @param did did
     * @return
     */
    public static String getDidFilterIMSI(String did) {
        try {
            String[] strs = getDidDataArray(did);
            return strs[1] + "|" + strs[2];
        } catch (Exception e) {
            logger.warn("获取getDidFilterIMSI出错", e);
            return null;
        }
    }

    /**
     * 从Did中获取IMSI
     * 
     * @param did deviceId
     * @return IMSI
     */
    public static String getIMSI(String did) {
        try {
            return getDidDataArray(did)[0];
        } catch (Exception e) {
            logger.warn("获取IMSI出错", e);
            return null;
        }
    }

    /**
     * 从Did中获取IMEI
     * 
     * @param did deviceId
     * @return IMEI
     */
    public static String getIMEI(String did) {
        try {
            return getDidDataArray(did)[1];
        } catch (Exception e) {
            logger.warn("获取IMEI出错", e);
            return null;
        }
    }

    /**
     * 是否为老格式（IMSI|IMEI|clientKey）的did
     * 
     * @param did
     * @return
     */
    public static boolean isOldFormatDid(String did) {
        return getDidDataArray(did) != null;
    }

    /**
     * 从did中解析出IMSI|IMEI|clientKey
     * 
     * @param did
     * @return IMSI|IMEI|clientKey数组
     */
    private static String[] getDidDataArray(String did) {
        String[] strs = StringUtil.split(did, '|');
        if (strs == null || strs.length != 3) {
            return null;
        }
        return strs;
    }

}
